package cn.desk.commons.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringInjection implements ApplicationContextAware {

	protected static ApplicationContext context;

	private String target;

	private InjectionInvoker injectionInvoker;

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		context = applicationContext;
	}

	public static ApplicationContext getContext() {
		return context;
	}

	public void init() {
		injectionInvoker.prepareInvoke();
		if (target == null) {
			throw new RuntimeException();
		}
		Object targetBean = context.getBean(target);
		if (targetBean == null) {
			throw new RuntimeException();
		}
		if (injectionInvoker == null) {
			throw new RuntimeException();
		}
		injectionInvoker.invoke(targetBean);
		injectionInvoker.afterInvoke();
	}

	public void setTarget(String str) {
		this.target = str;
	}

	public InjectionInvoker getInjectionInvoker() {
		return injectionInvoker;
	}

	public void setInjectionInvoker(InjectionInvoker injectionInvoker) {
		this.injectionInvoker = injectionInvoker;
	}
}
